package leetcode.周赛.第276场;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/1/16 14:39
 * @description：https://leetcode-cn.com/problems/maximum-running-time-of-n-computers/
 */
public class 同时运行N台电脑的最长时间 {

    @Test
    public void test() {
        Solution solution = new Solution();
    }

    class Solution {
        public long maxRunTime(int n, int[] batteries) {
            long sum = 0;
            Arrays.sort(batteries);
            for (int battery : batteries) {
                sum += battery;
            }
            for (int i = batteries.length - 1; i >= 0; i--) {
                if (batteries[i] > sum / n) {
                    n--;
                    sum -= batteries[i];
                } else {
                    return sum / n;
                }
            }
            return 0;
        }
    }
}
